﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lib.CommonForm;
using CustomUIControls;

namespace AUI
{
    public partial class frmDrugTypeInfo : FormBase
    {
        int _DrugTypeId = 0;
        DataRow m_dr;
        public frmDrugTypeInfo(DataRow dr)
        {
            InitializeComponent();
            

            this.txtDrugTypeCode.MaxLength = tbl_DrugTypes_Length.DrugTypeCode;
            this.txtDrugTypeName.MaxLength = tbl_DrugTypes_Length.DrugTypeName;

            _listRole = tbl_RoleRight_Controller.Instance().GetInfo("LoaiThuoc");
            FormGlobals.Control_SetRequired(txtDrugTypeCode, txtDrugTypeName);

            m_dr = dr;

            if (m_dr != null)
                _DrugTypeId = Globals.ToInt32(dr["DrugTypeId"]);

            LoadCombobox_DrugTypes();
            
            if (m_dr != null)
                FormGlobals.Panel_FillData(this, dr);

            _IsLoading = false;
        }

        public override void Show_Form()
        {
            this.ShowDialog();
        }
        void LoadCombobox_DrugTypes()
        {
            DataTable dtAll = tbl_DrugTypes_Controller.Instance().GetAll();

            DataView dv = dtAll.DefaultView;
            dv.RowFilter = "DrugTypeId <> " + _DrugTypeId.ToString();

            DataTable dt = new DataTable();
            dt = dv.ToTable();

            DataRow dr = dt.NewRow();
            dr["DrugTypeId"] = 0;
            dr["DrugTypeCode"] = "";
            dr["DrugTypeName"] = "";
            dt.Rows.InsertAt(dr, 0);

            MultiColumnComboBox box = (MultiColumnComboBox)cboParentId;
            box.DrawMode = DrawMode.OwnerDrawVariable;
            box.DropDownHeight = 400;
            box.FormattingEnabled = true;
            box.IntegralHeight = false;

            if (box.Width > 100)
                box.DisplayMember = "DrugTypeName";
            else
                box.DisplayMember = "DrugTypeCode";

            box.ValueMember = "DrugTypeId";
            box.ColumnDisplays = new string[] { "DrugTypeCode", "DrugTypeName" };
            box.DataSource = dt;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            tbl_DrugTypes_Info i = new tbl_DrugTypes_Info();
            FormGlobals.Panel_GetData(this, i);
            i.DrugTypeId = _DrugTypeId;
            i.CreateUser = FormGlobals.strUserName;
            i.LastUser = FormGlobals.strUserName;

            txtDrugTypeCode.BackColor = SystemColors.Window;
            if (tbl_DrugTypes_Controller.Instance().IsExist(i))
            {
                txtDrugTypeCode.BackColor = Color.Yellow;
                FormGlobals.Message_Warning(lblDrugTypeCode.Text + UIMessage.EXIST);
                txtDrugTypeCode.Focus();
                return;
            }

            _DrugTypeId = tbl_DrugTypes_Controller.Instance().Save(i);
            if (_DrugTypeId > 0)
            {
                FormGlobals.Message_InTaskbar(UIMessage.SAVE_COMPLETE);
                _dlgResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
